﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTOFrameworks;
using System.Collections;
using FooFun.DataElements.Dtos;

namespace PhysicalDataProvider
{
    public class PermissionService
    {
        string[] selectCols
            = new string[] { "PermissionId", "Permission" };

        string table = "t_Permissions";

        DEDataProvider dataProvider;

        public DEDataProvider DataProvider
        {
            get { return dataProvider; }
            set { dataProvider = value; }
        }

        public IDataElement ListPermission()
        {
            IDataElement dbDE
                = DataProvider.Select(selectCols, table, null);

            PermissionListDataElement list = new PermissionListDataElement(dbDE);

            return list;
        }

        public IDataElement ListDataElement()
        {
            IDataElement dbDE
                = DataProvider.Select(selectCols, table, null);

            return dbDE;
        }

        class PermissionDataElement : CustomDataElement, IEnumItem
        {
            public PermissionDataElement(IDataElement de)
                : base(de)
            {

            }

            public override IDataElement this[string attrName]
            {
                get
                {
                    switch (attrName)
                    {
                        case "PermissionId":
                            return DataProviderUtil.GetGuid
                                (base[attrName]);

                        default:
                            break;
                    }

                    return base[attrName];
                }
                set
                {
                    base[attrName] = value;
                }
            }


            #region IEnumItem 成员

            public string Name
            {
                get
                {
                    return Convert.ToString(
                        InnerDataElement["Permission"].Value);
                }
            }

            public Guid Id
            {
                get
                {
                    return DataProviderUtil.GetGuid(
                        InnerDataElement["PermissionId"].Value);
                }
            }

            #endregion
        }

        class PermissionListDataElement : CustomDataElement
        {
            public PermissionListDataElement(IDataElement de)
                : base(de)
            {

            }

            public override IEnumerator GetEnumerator()
            {
                foreach (IDataElement de in this.InnerDataElement)
                {
                    yield return new PermissionDataElement(de);
                }
            }
        }
    }
}
